<%--
  Created by IntelliJ IDEA.
  User: ttxs
  Date: 2023/7/31
  Time: 8:48
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="fm" uri="http://www.springframework.org/tags/form" %>
<html>
<head>
    <title>Title</title>
    <script src="${pageContext.request.contextPath}/statics/js/jquery.min.js"></script>
    <script>
        $(function () {
            var found = false;
            $("#account").blur(function () {
                let account = $(this).val();
                if (account != '') {
                    $.ajax({
                        url: '${pageContext.request.contextPath}/users/userExist',
                        type: 'get',
                        data: {account: account},
                        dataType: 'json',
                        success: function (result) {
                            found = result.found;
                            $("#msg").html(result.found + "," + result.msg);
                        },
                        error: function (err) {
                            alert('出错啦');
                        }
                    })
                }
            });

            $("form").submit(function () {
                /*if ($("[name=account]").val() == '') {
                    alert('账号不能为空');
                    return false;
                }*/
                if (found) {
                    alert('账号已经存在');
                    return false;
                }
                return true;
            })

            $.getJSON('${pageContext.request.contextPath}/roles/getAll', function (result) {
                for(let role of result) {
                    if (role.id == '${roleId}') {
                        $("[name=roleId]").append("<option selected value='" + role.id + "'>" + role.roleName + "</option>")
                    } else {
                        $("[name=roleId]").append("<option value='" + role.id + "'>" + role.roleName + "</option>")
                    }
                }
            })
        })
    </script>
</head>
<body>
<jsp:include page="/WEB-INF/jsp/header.jsp"/>
<h1>添加</h1>
<%--
 本页面使用了spring form标签，必须要设置modelAttribute
--%>
<fm:form modelAttribute="command"  method="post" enctype="multipart/form-data">
<table>
    <tr>
        <td>账号</td>
        <td>
            <fm:input path="account" id="account"/>
            <fm:errors path="account"/>
            <span id="msg"></span>
        </td>
    </tr>
    <tr>
        <td>昵称</td>
        <td>
            <fm:input path="realName"/>
            <fm:errors path="realName"/>
        </td>
    </tr>
    <tr>
        <td>生日</td>
        <td><input type="text" name="birthday">
            <fm:errors path="birthday"/>
        </td>
    </tr>
    <tr>
        <td>性别</td>
        <td><fm:input path="sex"/><fm:errors path="sex"/></td>
    </tr>
    <tr>
        <td>角色</td>
        <td>
            <select name="roleId">
                <option value="">请选择</option>
            </select>
        </td>
    </tr>
    <tr>
        <td>身份证</td>
        <td><input type="file" name="uploadFiles">
            <fm:errors path="idPicPath"/>
        </td>
    </tr>
    <tr>
        <td>工作照</td>
        <td><input type="file" name="uploadFiles">
            <fm:errors path="workPicPath"/>
        </td>
    </tr>
    <tr>
        <td colspan="2">
            <input value="添加" type="submit">
            <input value="返回" type="button" onclick="location.href='${pageContext.request.contextPath}/users/list'">
        </td>
    </tr>
</table>
</fm:form>
</body>
</html>
